// pages/elderly/info/editInfo/editInfo.js
const app = getApp()
const { formatTime } = require('../../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    createOrUpdate:null,
    type:null,
    elderlyname: '',
    familyname: '',
    childname: '',
    gender: null,
    age: '',
    phone: '',
    familyphone: '',
    address: '',
    buildingNumber:'',
    buildingArray:[
      '01',
      '02',
      '03',
      '04',
      '05',
      '06'
    ],
    roomNumber:'',
    roomArray:[
      '101',
      '102',
      '201',
      '202',
      '301',
      '302'
    ]
  },
  changeGender(e){
    this.setData({
      gender: parseInt(e.detail.value)
    })
  },
  nameChange(e){
    this.setData({
      familyname:e.detail.value
    })
  },
  elderlynameChange(e){
    this.setData({
      elderlyname:e.detail.value
    })
  },
  childnameChange(e){
    this.setData({
      childname:e.detail.value
    })
  },
  addressChange(e){
    this.setData({
      address:e.detail.value
    })
  },
  ageChange(e){
    this.setData({
      age:e.detail.value
    })
  },
  phoneChange(e){
    this.setData({
      phone:e.detail.value
    })
  },
  fphoneChange(e){
    this.setData({
      familyphone:e.detail.value
    })
  },
  changeBuilding:function(e){
    this.setData({
      buildingNumber:e.detail.value
    });
  },
  changeRoom:function(e){
    this.setData({roomNumber:e.detail.value});
    console.log(roomNumber)
  },
  changeType:function(e){
    this.setData({type:e.detail.value});
    console.log(this.type)
  },
  submit(){
    // let mes = '';
    // if(type == '家属')
    //   mes = '已经成功绑定'
    // else 
    //   mes = '您的id为78926'
    // wx.showToast({
      
    //   title: mes,
    //   icon: 'success',  // 图标类型，默认success
    //   duration: 1500  // 提示窗停留时间，默认1500ms
    // })
    // setTimeout(function () {
    //   wx.navigateTo({
    //     url: '/pages/elderly/elderly'
    //   })
    //  }, 1700)
    var that = this
    if(that.data.phone.trim() == '' || that.data.phone ==null){
      wx.showToast({
        title: '电话不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.address.trim() == '' ||that.data.address==null){
      wx.showToast({
        title: '地址不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.gender == null){
      wx.showToast({
        title: '性别不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(that.data.age == null){
      wx.showToast({
        title: '年龄不能为空',
        icon: 'error', 
        duration: 1500  
      })
      return;
    }
    if(this.data.type == 1){  // 家属
      if(that.data.familyname.trim() == '' || that.data.familyname==null){
        wx.showToast({
          title: '姓名不能为空',
          icon: 'error', 
          duration: 1500  
        })
        return;
      }
      var data = {
        familyname: that.data.familyname,
        gender: that.data.gender,
        phone: that.data.phone,
        address: that.data.address
      }
      app.sendPostRequest('/community/familyinfo', data, (res)=>{
        if(res.code==200){
          wx.showToast({
            title: '提交成功',
            duration: 1500,
            success:function () {
              setTimeout(function(){
                var pages = getCurrentPages(); //当前页面
                var beforePage = pages[pages.length - 2]; //前一页
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }
          })
        }
        if(res.code == 300){
          wx.showToast({
            title: '您已提交',
            icon:'error',
            duration: 1500,
            success:function () {
              setTimeout(function(){
                var pages = getCurrentPages(); //当前页面
                var beforePage = pages[pages.length - 2]; //前一页
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }
          })
        }
      }, (res)=>{
        console.log(res);
      })
    }
    if(this.data.type == 2){  // 老人
      if(that.data.elderlyname.trim() == '' || that.data.familyname==null){
        wx.showToast({
          title: '姓名不能为空',
          icon: 'error', 
          duration: 1500  
        })
        return;
      }
      if(that.data.familyphone.trim() == '' || that.data.familyphone ==null){
        wx.showToast({
          title: '家属电话不能为空',
          icon: 'error', 
          duration: 1500  
        })
        return;
      }
      var time = formatTime(new Date())
      var data = {
        elderlyname: that.data.elderlyname,
        gender: that.data.gender,
        phone: that.data.phone,
        age: parseInt(that.data.age),
        address: that.data.address,
        familyphone: that.data.familyphone,
        buildtime: time
      }
      app.sendPostRequest('/community/elderly', data, (res)=>{
        if(res.code==200){
          wx.showToast({
            title: '提交成功',
            duration: 1500,
            success:function () {
              setTimeout(function(){
                var pages = getCurrentPages(); //当前页面
                var beforePage = pages[pages.length - 2]; //前一页
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }
          })
        }
        if(res.code == 300){
          wx.showToast({
            title: '您已提交',
            icon:'error',
            duration: 1500,
            success:function () {
              setTimeout(function(){
                var pages = getCurrentPages(); //当前页面
                var beforePage = pages[pages.length - 2]; //前一页
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }
          })
        }
      }, (res)=>{
        console.log(res);
      })
    }
    if(this.data.type == 3){  // 儿童
      if(that.data.childname.trim() == '' || that.data.childname==null){
        wx.showToast({
          title: '姓名不能为空',
          icon: 'error', 
          duration: 1500  
        })
        return;
      }
      if(that.data.familyphone.trim() == '' || that.data.familyphone ==null){
        wx.showToast({
          title: '家属电话不能为空',
          icon: 'error', 
          duration: 1500  
        })
        return;
      }
      var time = formatTime(new Date())
      var data = {
        childname: that.data.childname,
        gender: that.data.gender,
        phone: that.data.phone,
        age: parseInt(that.data.age),
        address: that.data.address,
        familyphone: that.data.familyphone,
        createtime: time
      }
      app.sendPostRequest('/community/childinfo', data, (res)=>{
        if(res.code==200){
          wx.showToast({
            title: '提交成功',
            duration: 1500,
            success:function () {
              setTimeout(function(){
                var pages = getCurrentPages(); //当前页面
                var beforePage = pages[pages.length - 2]; //前一页
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }
          })
        }
        if(res.code == 300){
          wx.showToast({
            title: '您已提交',
            icon:'error',
            duration: 1500,
            success:function () {
              setTimeout(function(){
                var pages = getCurrentPages(); //当前页面
                var beforePage = pages[pages.length - 2]; //前一页
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }
          })
        }
      }, (res)=>{
        console.log(res);
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //  createOrUpdate 为1家属 2老人 3儿童
    var createOrUpdate = options.createOrUpdate
    this.setData({
      type:createOrUpdate
    })
    console.log("createOrUpdate",this.data.type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})